const database = require("../app/database");
class CommentServices {
  //根据文章查询所有评论
  async byessay(id) {
    const str = `
      select *,
        (select JSON_OBJECT('username',username,'id',user.id,'avatar',user.avatar) from user where id=cm.user_id) author,
        (select count(*) from commentthumb cmb where cmb.comment_id=cm.id) thumbcount  
      from comments cm where essay_id=? and comment_id is null order by cm.createAt desc;
		`;
    const str1 = `
      select *,
        (select JSON_OBJECT('username',username,'id',user.id,'avatar',user.avatar) from user where id=cm.user_id) author,
        (select count(*) from commentthumb cmb where cmb.comment_id=cm.id) thumbcount 
      from comments cm where comment_id=? and essay_id=? order by cm.createAt desc;
    `;
    const [result] = await database.execute(str, [id]);
    //递归查询
    const step = async (re, arr, replyname) => {
      const [r1] = await database.execute(str1, [re.id, id]);
      // const [[{ username }]] = await database.execute(re.id);
      for (let i = 0; i < r1.length; i++) {
        arr.push({ ...r1[i], replyname });
        await step(r1[i], arr, r1[i].author.username);
      }
    };
    //查询每个评论下的评论
    for (let i = 0; i < result.length; i++) {
      const arr = [];
      await step(result[i], arr, result[i].author.username);
      result[i].childcomment = arr.slice();
    }
    return {
      status: 200,
      result,
    };
  }

  //回复评论
  async reply(body) {
    const { content, userid, commentid, essayid } = body;
    const str = `
      insert into comments(content, user_id, comment_id,essay_id) values (?,?,?,?);
    `;
    await database.execute(str, [
      content,
      userid,
      commentid === -1 ? null : commentid,
      essayid,
    ]);
    return {
      status: 200,
      result: {
        message: "评论成功",
      },
    };
  }
  //删除评论
  async deletecom(id) {
    const str = `
     delete from comments where id=?;
    `;
    await database.execute(str, [id]);
    return {
      status: 200,
      result: {
        message: "删除评论成功",
      },
    };
  }
  //点赞评论或取消点赞
  async thumb(body) {
    const { type, userid, commentid } = body;
    const str1 = `
      delete from commentthumb where comment_id=? and user_id=?;
    `;
    const str2 = `
      insert into commentthumb(comment_id,user_id) values(?,?)
    `;
    switch (type) {
      case 0:
        await database.execute(str1, [commentid, userid]);
        break;
      case 1:
        await database.execute(str2, [commentid, userid]);
    }
    return {
      status: 200,
      result: {
        message: "成功",
      },
    };
  }
  //根据用户id查询评论
  async byuser(id) {
    const str = `
      select *,(select count(*) from commentthumb where comment_id=c.id) thumbcount from comments c where user_id=?;
		`;
    const [result] = await database.execute(str, [id]);
    return {
      status: 200,
      result,
    };
  }
  //管理员根据文章id查询评论
  async mangetbyessay(id) {
    const str = `
      select * from comments where essay_id=?
    `;
    const [result] = await database.execute(str, [id]);
    return {
      status: 200,
      result,
    };
  }
}
module.exports = new CommentServices();
